import vue from "@vitejs/plugin-vue";
import { defineConfig } from "vite";
import { resolve } from "path";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import legacy from "@vitejs/plugin-legacy";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

export default defineConfig({
  base: "/",
  plugins: [
    vue(),
    legacy({
      targets: ["ie>=11"],
      additionalLegacyPolyfills: ["regenerator-runtime/runtime"],
    }),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],

  test: {
    reporters: ["default", "html"],
    coverage: {
      enabled: true,
      provider: "v8",
      cleanOnRerun: true,
      reporter: ["text", "json", "html"],
    },
  },
  server: {
    port: 2000,
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Expose-Headers":
        "Content-Length,Content-Range,Set-Cookie",
    },

    proxy: {
      "/apis": {
        target: "https://tu.h0.cn",
        changeOrigin: true,
        followRedirects: true,
        rewrite: (path) => path.replace(/^\/apis/, ""),
      },
    },
    host: "0.0.0.0",
  },
  resolve: {
    alias: {
      "@": resolve(__dirname, ".", "./src/"),
    },
  },
  build: {
    outDir: "dist",
    chunkSizeWarningLimit: 1500,
    rollupOptions: {
      output: {
        chunkFileNames: "assets/js/[name]-[hash].js",
        entryFileNames: "assets/js/[name]-[hash].js",
        assetFileNames: "assets/[ext]/[name]-[hash].[ext]",
        manualChunks: function (id) {
          if (id.includes("node_modules")) {
            let match = id
              .toString()
              .match(/\/node_modules\/(?!.pnpm)(?<moduleName>[^\/]*)\//);
            return (
              (match && match.groups && match.groups.moduleName) || "vender"
            );
          }
        },
      },
    },
  },
});
